angular.module('app').config(function ($stateProvider) {

    $stateProvider.state('app.contacts', {
        url         : '/contacts',
        label       : '联系人信息',
        controller  : 'app.contacts',
        controllerAs: '$ctrl',
        templateUrl : 'view/app/contacts/contacts.html'
    });
}).controller('app.contacts', function ($timeout) {
    var ctrl = this;
    ctrl.options = {
        filter        : '',
        multiple      : false,
        // desiredFields : '',
        hasPhoneNumber: false
    };
    ctrl.save = function () {
        /**
         * navigator.contacts.create(Contact)
         * 该方法只是返回一个 Contact并不会保存到设备中, 需要用返回的 Contact 的 save 方法才会保存到设备中
         * - Contact
         *   属性
         *   - id: (string)全局唯一 id
         *   - displayName: (string) 用户名称
         *   - name: (ContactName). 详见 https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-contacts/index.html#contactname
         *   - nickname: (string)
         *   - phoneNumbers: (ContactField[]). 详见 https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-contacts/index.html#contactfield
         *   - emails: (ContactField[]). 详见 https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-contacts/index.html#contactfield
         *   - addresses: (ContactAddress[]). 详见 https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-contacts/index.html#contactaddress
         *   - ims: (ContactField[]). 详见 https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-contacts/index.html#contactfield
         *   - organizations: (ContactOrganization[]). 详见 https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-contacts/index.html#contactorganization
         *   - birthday: (Date)生日
         *   - note: (string)备注
         *   - photos: (ContactField[]). 详见 https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-contacts/index.html#contactfield
         *   - categories: (ContactField[]). 详见 https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-contacts/index.html#contactfield
         *   - urls: (ContactField[]). 详见 https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-contacts/index.html#contactfield
         *   方法
         *   - clone: 返回一个深度复制的 Contact 但是 id 为 null. contact.clone();
         *   - remove: 从设备中删除这个 Contact. contact.remove(onSuccess,onError);
         *   - save: 添加&更新设备中的 Contact(根据 id 是否相同区分). contact.save(onSuccess,onError);
         */
        var myContact = navigator.contacts.create({
            displayName: ctrl.user.name
        });
        myContact.save(function () {
            alert('添加成功');
        }, errorCallback);
    };
    ctrl.find = function () {
        /**
         * navigator.contacts.find(contactFields, contactSuccess, contactError, contactFindOptions)
         * 查询多条
         * - contactFields: '*' 表示所有, 或则 navigator.contacts.fieldType 的一个筛选数组
         * - contactFindOptions
         *   - filter: 忽略大小写的一个筛选字符串
         *   - multiple: 确定是否查找操作返回多个 navigator.contacts, 默认 false
         *   - desiredFields: 指定返回的 Contact 中包含的 fields, 不指定则返回所有的 fields
         *   - hasPhoneNumber: (只支持 Android)返回只包含电话号码的 Contact, 默认 false
         *
         */
        navigator.contacts.find(['*'], function (contacts) {
            // 此处的 $timeout 就相当于 $scope.$digest() 效果
            $timeout(function () {
                ctrl.findResult = contacts;
            });
        }, errorCallback, ctrl.options);
    };
    ctrl.pick = function () {
        /**
         * navigator.contacts.pickContact(contactSuccess, contactError)
         * 单选
         */
        navigator.contacts.pickContact(function (contact) {
            $timeout(function () {
                ctrl.pickResult = contact;
            });
        }, errorCallback);
    };

    /**
     * ContactError
     *   ContactError.UNKNOWN_ERROR (code 0)
     *   ContactError.INVALID_ARGUMENT_ERROR (code 1)
     *   ContactError.TIMEOUT_ERROR (code 2)
     *   ContactError.PENDING_OPERATION_ERROR (code 3)
     *   ContactError.IO_ERROR (code 4)
     *   ContactError.NOT_SUPPORTED_ERROR (code 5)
     *   ContactError.OPERATION_CANCELLED_ERROR (code 6)
     *   ContactError.PERMISSION_DENIED_ERROR (code 20)
     */

    function errorCallback(ContactError) {
        alert(JSON.stringify(ContactError));
    }
});